<%- include('head') -%>

  <body>
    <div>
      <span class="layui-breadcrumb">
        <a href="/dashboard">首页</a>
        <a><cite>商品管理</cite></a>
      </span>
    </div>
    <a style="margin-top: 20px" href="/itemAdd" class="layui-btn layui-btn-warm">增加商品</a>
    <table class="layui-table layui-form">
      <thead>
        <tr>
          <th>商品名称</th>
          <th>描述</th>
          <th>价格</th>
          <th>库存</th>
          <th>上下架</th>
          <th>上架时间</th>
          <th>封面</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <% for(var i=0;i<items.length;i++) { %>
          <tr>
            <td>
              <%= items[i].itemName %>
            </td>
            <td title="<%=  items[i].itemDesc %>">
              <%= items[i].itemDesc ? items[i].itemDesc.length> 10? items[i].itemDesc.substr(0, 10) + '...' :
                items[i].itemDesc
                :
                '暂无描述'
                %>
            </td>
            <td>
              <%= items[i].itemPrice %>
            </td>
            <td>
              <%= items[i].itemStore %>
            </td>
            <td>
              <!-- <%= items[i].onSale == 0? '上架': '下架' %> -->
              <input type="checkbox" disabled <%=items[i].onSale==0? 'checked' : '' %> lay-skin="switch"
              lay-filter="switchTest" lay-text="上架|下架">
            </td>
            <td>
              <%= items[i].createAt %>
            </td>
            <td>
              <img
                src="<%= items[i].thumb?items[i].thumb:'https://img0.baidu.com/it/u=167397068,3164312087&fm=26&fmt=auto' %>"
                width="80" alt="">
            </td>
            <td>
              <a href="/itemUpdate?id=<%= items[i]._id %>" class="layui-btn layui-btn-xs">编辑</a>
              <button data-id="<%= items[i]._id %>" class="layui-btn delete layui-btn-danger layui-btn-xs">删除</button>
            </td>
          </tr>
          <%}%>
      </tbody>
    </table>

    <div id="page"></div>
    <script src="/static/layui/layui.js"></script>
    <script>
      layui.use(['laypage', 'jquery', 'layer'], function () {
        var laypage = layui.laypage;
        var $ = layui.jquery;
        var layer = layui.layer;
        // 删除商品
        $('.delete').click(function () {
          var id = this.dataset.id
          layer.confirm('您确认删除嘛?', { icon: 3, title: '删除商品' }, function (index) {
            $.ajax({
              url: '/delItem',
              data: {
                id: id
              },
              success: function (res) {
                if (res.code === 0) {
                  layer.msg(res.msg, {
                    icon: 1,
                    time: 1000
                  }, function () {
                    // 刷新页面
                    history.go(0)
                  })
                }else{
                  layer.msg(res.msg, {
                    icon: 2,
                    time: 1000
                  })
                }
              }
            })
            layer.close(index);
          });
        })
        laypage.render({
          elem: 'page',
          curr: <%= page %>,
          count:  <%= counts %>,
          limit: 2,
          jump: function (obj, first) {

            //首次不执行
            if (!first) {
              //do something
              location.href = "/itemLists?page=" + obj.curr + '&pageSize=' + obj.limit;
            }
          }
    })
  })
    </script>
  </body>

  </html>